<template>
    <view class="content">
        <view class="search-box">
			<u-search placeholder="搜索内容" shape="square" :show-action="false" bg-color="#f3f4f5" search-icon-color="#1f3b67"
				v-model="keyword" @search="doSearch"></u-search>
        </view>
        <view class="wrapper">
            <!-- 左侧导航 -->
            <scroll-view scroll-y="true" class="left-box">
            	<view class="left" v-for="(item, index) in cate" :key="index"
            	    :class="{active : item.id == parentId}" @click="changeCate(item, index)">
            	    {{item.name}}
            	</view>
            </scroll-view>
			
            <!-- 右侧 -->
            <scroll-view scroll-y="true" class="right-box" @scrolltolower="getList">
				<view class="right" v-for="(item, index) in list" :key="index" 
					@click="$gTo(`/pages/goods/goods-details?id=${item.id}&good_type=${item.good_type}`)">
					
					<image :src="item.image_uri" class="del-05"></image>
				    <view class="u-flex-1">
				        <view class="goods-name u-line-2">{{item.belong}}</view>
				        <view class="sub" v-if="item.content">{{item.content}}</view>
						<view class="item-down">
				            <view class="price">
				               ￥<text class="price-num">{{item.price}}</text>
				            </view>
							<view class="line-price" v-if="item.material_price">
								￥{{item.material_price}}
							</view>
				        </view>    
				    </view>
				</view>
				
                <view style="height: 94rpx;padding-top: 30rpx;">
                    <u-loadmore :status="status" v-if="status != 'loadmore'" />
                </view>
            </scroll-view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                keyword: '',
                parentId: '',
                cate: [], //左侧分类导航
                status: 'loadmore', //是否显示没有更多了
                page: 1,
                list: [], 
            };
        },
        onLoad() {
            this.getCate()
        },
        methods: {
            // 搜索
            doSearch() {
                this.getCate()
            },
			
            // 切换左侧分类导航
            changeCate(item, index) {
                if (this.parentId == item.id) return;
                this.parentId = item.id;
                this.page = 1
                this.list = []
                this.status = 'loadmore'
                this.getList()
            },
			
            // 获取商品列表
            getList() {
            	if (this.status == 'nomore') return;
            	this.status = 'loading';
            	this.$ajax('applets/wareList', {
            		userToken: this.$getSync('userToken'),
            		page: this.page,
            		limit: 10,
                    type: 2,//2-正常商品 3-积分商品
					is_recommend: 0,//是否首页推荐 0-否 1-是
                    category_id: this.parentId,//分类编号
                    keywords: this.keyword,//搜索关键词
            	}).then(ret => {
            		if (ret.status == 0) {
            			if (ret.data.length > 0) {
            				this.list = this.list.concat(ret.data);
            				this.page++;
            				this.status = 'loadmore'
            			} else {
            				this.status = 'nomore'
            			}
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
			
            // 获取分类
            getCate(){
            	this.$ajax('applets/category', {
                    userToken: this.$getSync('userToken'),
            	}).then(ret => {
            		if (ret.status == 0) {
                        this.cate = ret.data;
						
                        if (!this.parentId && ret.data.length > 0) {
                            this.parentId = ret.data[0].id;
                        }
                        this.page = 1
                        this.list = []
                        this.status = 'loadmore'
                        this.getList()
            		} else {
            			this.$toast(ret.message);
            		}
            	});
            },
        }
    };
</script>

<style lang="scss">
    
    page{
        background-color: #ffffff;
    }
    .search-box{
        padding: 20rpx;
    }
    
    .wrapper{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .left-box{
        height: calc(100vh - 105rpx);
    	width: 22vw;
        background-color: #f6f6f6;
    }
    .left {
    	width: 100%;
		padding: 30rpx 14rpx;
    	display: flex;
    	justify-content: center;
    	align-items: center;
    	font-size: 26upx;
    	box-sizing: border-box;
    }
    .active {
    	background-color: #fff;
    	box-sizing: border-box;
        color: #2558ab;
        font-size: 28rpx;
        font-weight: bold;
        position: relative;
    }
    .active::after{
		display: block;
		content: '';
		width: 8rpx;
		height: 32rpx;
		background-color: #2558ab;
		position: absolute;
		left: 0;
		top: 50%;
		transform: translateY(-50%);
	}
    .right-box{
        width: 78vw;
        height: calc(100vh - 105rpx);
        padding: 16rpx 0;
    }
    .right{
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #fff;
		padding: 25rpx 20rpx;
		border-radius: 20rpx;
		box-shadow: 2px 1px 15px #efefef;
		margin: 0 16rpx 25rpx;
    }
    .del-05{
        width: 166rpx;
        height: 166rpx;
        border-radius: 10rpx;
        border: 2rpx solid #e6e6e6;
        margin-right: 20rpx;
    }
    .goods-name{
        font-size: 28rpx;
        font-weight: bold;
    }
	.sub{
		font-size: 22rpx;
		color: #54586a;
		padding-top: 5rpx;
		width: 325rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
    .item-down{
        display: flex;
        justify-content: space-between;
        align-items: center;
		padding-top: 20rpx;
    }
    .price{
        color: #f62a2a;
        font-size: 22rpx;
    }
    .price-num{
        font-size: 38rpx;
        font-weight: bold;
    }
    .line-price{
        font-size: 22rpx;
        color: #6a6e7e;
		text-decoration: line-through;
    }
    
    .none{
    	text-align: center;
    	padding: 50upx 0;
    	box-sizing: border-box;
    	color: #999;
    	font-size: 24upx;
    }

</style>
